import { userAccountLogin } from '@/api/login'
import router from '@/router'
// 用户模块
export default {
  namespaced: true,
  state: {
    // 用户信息
    profile: {
      id: '',
      avatar: '',
      nickname: '',
      account: '',
      mobile: '',
      token: null

    }
  },
  // 谬腿行死
  mutations: {
    // 修改用户信息，payload就是用户信息对象
    setUser (state, payload) {
      state.profile = payload
      console.log(state.profile)

      router.push('/')
    },
    setToken (state, payload) {
      state.token = payload
    }
  },

  actions: {
    // 基于账号的登录
    async accountLogin (state, payload) {
      try {
        const ret = await userAccountLogin(payload)
        if (ret && ret.result) {
          // 登录成功，缓存token

          const { id, avatar, nickname, account, mobile, token } = ret.result
          state.commit('setUser', { id, avatar, nickname, account, mobile, token })

          return true
        } else {
          // 登录失败
          return false
        }
      } catch {
        return false
      }
    }
  },
  getters: {
    gettoken (state) {
      return state.token
    }
  }
}
